package cac.fenjian.resource.AoTask.MsTaskPackage.util;

import cac.fenjian.resource.AoTask.MsTaskPackage.dao.MsTaskCheckOperDao;
import com.cac.dme.adapter.rest.msg.ResponseMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;

@Component
public class MsTaskCheckOperUtil {
    @Autowired
    private MsTaskCheckOperDao msTaskCheckOperDao;
    /**
     * 检查当前扫码的缓存箱
     */
    public ResponseMsg checkSacnHcx(String indexTm){
        ResponseMsg responseMsg = new ResponseMsg();
        responseMsg.setRetCode("201");
        responseMsg.setErrorDesc("N");
        String ip = null;
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        if (request.getHeader("x-forwarded-for") == null) {
            ip = request.getRemoteAddr();
        } else {
            ip = request.getHeader("x-forwarded-for");
        }
        //正在执行的缓存箱是否回库，或分拣保存
        HashMap curHcx = msTaskCheckOperDao.queryPreHcxIsBack(ip,indexTm);
        if(curHcx == null){//当前操作记录没有执行的缓存箱
            responseMsg.setRetCode("200");
            return responseMsg;
        }
        //当前缓存箱是否需要回库
        if("Y".equals(curHcx.get("isSm").toString())&&"Y".equals(curHcx.get("preHcxBackState").toString())){
            responseMsg.setMessage((String)curHcx.get("msg"));
            return responseMsg;
        }
        //扫码的箱没有问题后，判断当前工作包是否需要回库
        //查询当前执行的工作包是否需要回库
        HashMap curPackage = msTaskCheckOperDao.queryCurPackageAfterScan(ip);
        if("Y".equals(curPackage.get("isNeedBack").toString())){
            responseMsg.setRetCode("201");
            responseMsg.setMessage("当前工作包需要回库");
            responseMsg.setErrorDesc("Y");
        }
        responseMsg.setRetCode("200");
        return responseMsg;
    }

    /**
     * 检查扫码的工作包
     * */
    public ResponseMsg checkScanPackage(String indexTm){
        ResponseMsg responseMsg = new ResponseMsg();
        responseMsg.setRetCode("200");
        responseMsg.setErrorDesc("N");
        String ip = null;
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        if (request.getHeader("x-forwarded-for") == null) {
            ip = request.getRemoteAddr();
        } else {
            ip = request.getHeader("x-forwarded-for");
        }

        //查询当前工作包是否需要回库
        HashMap curPackage = msTaskCheckOperDao.queryCurPackageAfterScan(ip);
        if(curPackage==null||curPackage.isEmpty()){
            return responseMsg;
        }
        if("Y".equals(curPackage.get("isNeedBack").toString())){
            responseMsg.setRetCode("200");
            responseMsg.setMessage(curPackage.get("palletCode")+"当前工作包需要回库");
            responseMsg.setErrorDesc("Y");
        }else if(!indexTm.equals(curPackage.get("palletCode"))){
            responseMsg.setRetCode("201");
            responseMsg.setMessage(curPackage.get("palletCode")+"工作包物料还未装完");
            responseMsg.setErrorDesc("N");
        }
        return responseMsg;
    }

}
